package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.dto.excution.PUserMgtExcution;
import com.fortune.dto.in.pUserMgt.PUserAddDto;
import com.fortune.dto.in.pUserMgt.PUserEditDto;
import com.fortune.dto.in.pUserMgt.PUserListDto;
import com.fortune.dto.in.pUserMgt.PUserLogableDto;
import com.fortune.entity.PPosition;
import com.fortune.enums.PUserMgtEnum;
import com.fortune.service.PUserMgtService;
import com.fortune.vo.pUserMgt.PUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(value = "puser", description="平台员工管理", tags = {"平台员工管理"})
@RestController
@RequestMapping("pUserMgt")
@Validated
public class PUserMgtController {
	@Autowired
	private PUserMgtService pUserMgtService;


	@ApiOperation(value = "平台员工列表", nickname = "mUserList", notes = "@石匡")
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
		@ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
		@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
	@GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<PUser>> list(@Validated @ApiIgnore PUserListDto listDto) {
		listDto.setPage((listDto.getPage()-1)*10);
		PagingData<PUser> pagingData = pUserMgtService.pagingData(listDto);
		return new PUserMgtExcution<PagingData<PUser>>(pagingData, PUserMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "添加平台员工", nickname = "pUserAdd", notes = "@石匡")
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(@Validated @RequestBody PUserAddDto addDto) {
		int result = pUserMgtService.addPUser(addDto);
		if (result==2) {
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.PHONEFALSE);
		}else if (result==3) {
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.NUMBERFALSE);
		}else if (result==1) {
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.SUCCESS);
		} else {
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.FAILSE);
		}
	}

	@ApiOperation(value = "编辑平台员工", nickname = "pUserEdit", notes = "@石匡")
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(@Validated @RequestBody PUserEditDto editDto) {
		int result = pUserMgtService.edit(editDto);
		if (result==2) {
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.CHECKUSER);
		}else if (result==3) {
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.CHECKPHONE);
		}else if (result==1) {
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.SUCCESS);
		}else if (result == 5){
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.NUMBER_EXIST);
		}else {
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.FAILSE);
		}
	}

	@ApiOperation(value = "删除平台员工", nickname = "pUserDelete", notes = "@石匡")
	@ApiImplicitParam(name="userId", value="用户id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(@Validated @NotBlank(message="用户id不能为空") String userId) {
		int result = pUserMgtService.queryIsExist(userId);
		if (result < 1){
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.CHECKUSER);
		}
		result = pUserMgtService.delete(userId);
		if(result==0)
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.FAILSE);
		return new PUserMgtExcution<Void>(null, PUserMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "重置平台员工密码", nickname = "pUserResetPassword", notes = "@石匡")
	@ApiImplicitParam(name="userId", value="用户id",paramType="query",dataType="string")
    @PutMapping(value = "resetPassword",
            produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> resetPassword(@Validated @NotBlank(message="用户id不能为空") String userId) {
		int result = pUserMgtService.resetPassword(userId);
		if(result==0)
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.FAILSE);
		return new PUserMgtExcution<Void>(null, PUserMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "设置平台员工是否允许登录", nickname = "pUserLogableSetting", notes = "@石匡")
    @PutMapping(value = "logableSetting",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> logableSetting(@Validated @RequestBody PUserLogableDto logableDto) {
		int result = pUserMgtService.setLogin(logableDto);
		if(result==0)
			return new PUserMgtExcution<Void>(null, PUserMgtEnum.FAILSE);
		return new PUserMgtExcution<Void>(null, PUserMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "平台员工岗位列表", nickname = "mUserPositionList", notes = "@石匡")
	@GetMapping(value = "mUserPositionList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<PPosition>> mUserPositionList() {
		List<PPosition> list = pUserMgtService.queryPositionList();
		return new PUserMgtExcution<List<PPosition>>(list, PUserMgtEnum.SUCCESS);
	}

}
